/**
 * 
 */
package com.cp16.tss.form;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import com.cp16.tss.handler.RequirementHandler;
import com.toedter.calendar.JDateChooser;

/**
 * @author ADMIN
 *
 */
public class RequirementForm extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTable tblResultSearch;
	private JPanel panSearch;
	private JLabel lblListForRequirement;
	private JPanel panResult;
	private JButton btnClose;
	private JButton btnEdit;
	private JButton btnDelete;
	private JLabel lblCustomerId;
	private JTextField txtCustomerId;
	private JLabel lblStatus;
	private JButton btnSearch;
	private JButton btnAdd;
	private JLabel lblTotalRequirement;
	private JButton btnRefresh;
	private JScrollPane scrollResult;
	private JButton btnCancel;
	@SuppressWarnings("rawtypes")
	private JComboBox cbStatus;
	private ButtonGroup rdbDateChooser;
	private JRadioButton rdbtnDateRequirement;
	private JRadioButton rdbtnDeadline;
	private JRadioButton rdbtnDateRelease;
	private JLabel lblDateFrom;
	private JDateChooser txtDateTo;
	private JLabel lblDateTo;
	private JDateChooser txtDateFrom;

	/**
	 * Create the frame.
	 */
	public RequirementForm() {
		loadLookAndFeel();
		initComponents();
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void initComponents() {

		setTitle("CustomerCare- created by Hoang Ngoc Giang");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLocation(150,70);
		setSize(980, 650);
		setResizable(false);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(102, 102, 204));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		panSearch = new JPanel();
		panSearch.setBounds(10, 11, 954, 143);
		contentPane.add(panSearch);
		panSearch.setLayout(null);

		lblListForRequirement = new JLabel("Search for requirement");
		lblListForRequirement.setBounds(373, 11, 291, 30);
		lblListForRequirement.setFont(new Font("Tahoma", Font.PLAIN, 22));
		panSearch.add(lblListForRequirement);
		
		txtCustomerId = new JTextField();
		txtCustomerId.setBounds(205, 54, 112, 29);
		txtCustomerId.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtCustomerId.setColumns(10);
		panSearch.add(txtCustomerId);
		
		lblCustomerId = new JLabel("Customer ID");
		lblCustomerId.setBounds(95, 54, 89, 29);
		lblCustomerId.setFont(new Font("Tahoma", Font.PLAIN, 15));
		panSearch.add(lblCustomerId);
		
		lblStatus = new JLabel("Status");
		lblStatus.setBounds(95, 106, 57, 29);
		lblStatus.setFont(new Font("Tahoma", Font.PLAIN, 15));
		panSearch.add(lblStatus);

		panResult = new JPanel();
		panResult.setBounds(10, 165, 954, 446);
		contentPane.add(panResult);
		panResult.setLayout(null);
		
		scrollResult = new JScrollPane();
		scrollResult.setBounds(10, 11, 934, 391);
		panResult.add(scrollResult);

		Vector rowHeader = new Vector();
		rowHeader.add("No");
		rowHeader.add("REQ ID");
		rowHeader.add("Customer ID");
		rowHeader.add("Customer name");
		rowHeader.add("Service");
		rowHeader.add("Date REQ");
		rowHeader.add("Deadline");
		rowHeader.add("Status");
		rowHeader.add("Date Release");
		DefaultTableModel defaultTableModel = new DefaultTableModel(rowHeader, 0);
		
		tblResultSearch = new JTable();
		tblResultSearch.setBounds(10, 11, 934, 386);
		tblResultSearch.setFont(new Font("Tahoma", Font.PLAIN, 13));
		tblResultSearch.setModel(defaultTableModel);
		
		tblResultSearch.getColumnModel().getColumn(0).setPreferredWidth(50);
		tblResultSearch.getColumnModel().getColumn(0).setPreferredWidth(50);
		tblResultSearch.getColumnModel().getColumn(2).setPreferredWidth(80);
		tblResultSearch.getColumnModel().getColumn(3).setPreferredWidth(180);
		tblResultSearch.getColumnModel().getColumn(4).setPreferredWidth(180);
		tblResultSearch.getColumnModel().getColumn(5).setPreferredWidth(80);
		tblResultSearch.getColumnModel().getColumn(6).setPreferredWidth(80);
		
		scrollResult.setViewportView(tblResultSearch);
		
		lblTotalRequirement = new JLabel("");
		lblTotalRequirement.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblTotalRequirement.setBounds(765, 409, 179, 29);
		panResult.add(lblTotalRequirement);
		
		btnSearch = new JButton("Search");
		btnSearch.setBounds(846, 60, 77, 29);
		btnSearch.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnSearch.addActionListener(this);
		panSearch.add(btnSearch);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnCancel.setBounds(846, 100, 77, 29);
		btnCancel.addActionListener(this);
		panSearch.add(btnCancel);
		
		cbStatus = new JComboBox();
		cbStatus.setBounds(205, 106, 112, 29);
		cbStatus.addItem("All");
		cbStatus.addItem("Request");
		cbStatus.addItem("Resolving");
		cbStatus.addItem("Resolved");
		cbStatus.addItem("Re-open");
		cbStatus.addItem("Cancel");
		panSearch.add(cbStatus);
		
		rdbDateChooser = new ButtonGroup();
		
		rdbtnDateRequirement = new JRadioButton("Date requirement");
		rdbtnDateRequirement.setFont(new Font("Tahoma", Font.PLAIN, 15));
		rdbtnDateRequirement.setBounds(405, 57, 139, 23);
		panSearch.add(rdbtnDateRequirement);
		
		rdbtnDeadline = new JRadioButton("Deadline");
		rdbtnDeadline.setFont(new Font("Tahoma", Font.PLAIN, 15));
		rdbtnDeadline.setBounds(405, 83, 139, 23);
		panSearch.add(rdbtnDeadline);
		
		rdbtnDateRelease = new JRadioButton("Date release");
		rdbtnDateRelease.setFont(new Font("Tahoma", Font.PLAIN, 15));
		rdbtnDateRelease.setBounds(405, 109, 139, 23);
		panSearch.add(rdbtnDateRelease);
		
		rdbDateChooser.add(rdbtnDateRequirement);
		rdbDateChooser.add(rdbtnDeadline);
		rdbDateChooser.add(rdbtnDateRelease);
		
		lblDateFrom = new JLabel("From");
		lblDateFrom.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblDateFrom.setBounds(583, 54, 49, 29);
		panSearch.add(lblDateFrom);
		
		txtDateTo = new JDateChooser();
		txtDateTo.setDateFormatString("dd/MM/yyyy");
		txtDateTo.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtDateTo.setBounds(634, 105, 130, 30);
		panSearch.add(txtDateTo);
		
		lblDateTo = new JLabel("To");
		lblDateTo.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblDateTo.setBounds(583, 106, 37, 29);
		panSearch.add(lblDateTo);
		
		txtDateFrom = new JDateChooser();
		txtDateFrom.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtDateFrom.setDateFormatString("dd/MM/yyyy");
		txtDateFrom.setBounds(634, 53, 130, 30);
		panSearch.add(txtDateFrom);
		
		btnAdd = new JButton("Add");
		btnAdd.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnAdd.setBackground(new Color(230, 230, 250));
		btnAdd.setBounds(361, 408, 83, 30);
		btnAdd.addActionListener(this);
		panResult.add(btnAdd);

		btnClose = new JButton("Close");
		btnClose.setBounds(611, 408, 83, 30);
		btnClose.setBackground(new Color(230, 230, 250));
		btnClose.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnClose.addActionListener(this);
		panResult.add(btnClose);

		btnEdit = new JButton("Edit");
		btnEdit.setBounds(444, 408, 83, 30);
		btnEdit.setBackground(new Color(230, 230, 250));
		btnEdit.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnEdit.addActionListener(this);
		panResult.add(btnEdit);

		btnDelete = new JButton("Delete");
		btnDelete.setBounds(527, 408, 83, 30);
		btnDelete.setBackground(new Color(230, 230, 250));
		btnDelete.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnDelete.addActionListener(this);
		panResult.add(btnDelete);
		
		btnRefresh = new JButton("Refresh");
		btnRefresh.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnRefresh.setBackground(new Color(230, 230, 250));
		btnRefresh.setBounds(278, 408, 83, 30);
		btnRefresh.addActionListener(this);
		panResult.add(btnRefresh);
	}

	private void loadLookAndFeel() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException ex) {
			Logger.getLogger(RequirementForm.class.getName()).log(Level.SEVERE,
					null, ex);
		} catch (InstantiationException ex) {
			Logger.getLogger(RequirementForm.class.getName()).log(Level.SEVERE,
					null, ex);
		} catch (IllegalAccessException ex) {
			Logger.getLogger(RequirementForm.class.getName()).log(Level.SEVERE,
					null, ex);
		} catch (UnsupportedLookAndFeelException ex) {
			Logger.getLogger(RequirementForm.class.getName()).log(Level.SEVERE,
					null, ex);
		}
	}

	/**
	 * @return the tblResultSearch
	 */
	public JTable getTblResultSearch() {
		return tblResultSearch;
	}

	/**
	 * @param tblResultSearch the tblResultSearch to set
	 */
	public void setTblResultSearch(JTable tblResultSearch) {
		this.tblResultSearch = tblResultSearch;
	}

	/**
	 * @return the lblListForRequirement
	 */
	public JLabel getLblListForRequirement() {
		return lblListForRequirement;
	}

	/**
	 * @param lblListForRequirement the lblListForRequirement to set
	 */
	public void setLblListForRequirement(JLabel lblListForRequirement) {
		this.lblListForRequirement = lblListForRequirement;
	}

	/**
	 * @return the btnClose
	 */
	public JButton getBtnClose() {
		return btnClose;
	}

	/**
	 * @param btnClose the btnClose to set
	 */
	public void setBtnClose(JButton btnClose) {
		this.btnClose = btnClose;
	}

	/**
	 * @return the btnEdit
	 */
	public JButton getBtnEdit() {
		return btnEdit;
	}

	/**
	 * @param btnEdit the btnEdit to set
	 */
	public void setBtnEdit(JButton btnEdit) {
		this.btnEdit = btnEdit;
	}

	/**
	 * @return the btnDelete
	 */
	public JButton getBtnDelete() {
		return btnDelete;
	}

	/**
	 * @param btnDelete the btnDelete to set
	 */
	public void setBtnDelete(JButton btnDelete) {
		this.btnDelete = btnDelete;
	}

	/**
	 * @return the lblCustomerId
	 */
	public JLabel getLblCustomerId() {
		return lblCustomerId;
	}

	/**
	 * @param lblCustomerId the lblCustomerId to set
	 */
	public void setLblCustomerId(JLabel lblCustomerId) {
		this.lblCustomerId = lblCustomerId;
	}

	/**
	 * @return the txtCustomerId
	 */
	public JTextField getTxtCustomerId() {
		return txtCustomerId;
	}

	/**
	 * @param txtCustomerId the txtCustomerId to set
	 */
	public void setTxtCustomerId(JTextField txtCustomerId) {
		this.txtCustomerId = txtCustomerId;
	}

	/**
	 * @return the lblStatus
	 */
	public JLabel getLblStatus() {
		return lblStatus;
	}

	/**
	 * @param lblStatus the lblStatus to set
	 */
	public void setLblStatus(JLabel lblStatus) {
		this.lblStatus = lblStatus;
	}

	/**
	 * @return the btnSearch
	 */
	public JButton getBtnSearch() {
		return btnSearch;
	}

	/**
	 * @param btnSearch the btnSearch to set
	 */
	public void setBtnSearch(JButton btnSearch) {
		this.btnSearch = btnSearch;
	}

	/**
	 * @return the btnAdd
	 */
	public JButton getBtnAdd() {
		return btnAdd;
	}

	/**
	 * @param btnAdd the btnAdd to set
	 */
	public void setBtnAdd(JButton btnAdd) {
		this.btnAdd = btnAdd;
	}

	/**
	 * @return the lblTotalRequirement
	 */
	public JLabel getLblTotalRequirement() {
		return lblTotalRequirement;
	}

	/**
	 * @param lblTotalRequirement the lblTotalRequirement to set
	 */
	public void setLblTotalRequirement(JLabel lblTotalRequirement) {
		this.lblTotalRequirement = lblTotalRequirement;
	}

	/**
	 * @return the btnRefresh
	 */
	public JButton getBtnRefresh() {
		return btnRefresh;
	}

	/**
	 * @param btnRefresh the btnRefresh to set
	 */
	public void setBtnRefresh(JButton btnRefresh) {
		this.btnRefresh = btnRefresh;
	}

	/**
	 * @return the scrollResult
	 */
	public JScrollPane getScrollResult() {
		return scrollResult;
	}

	/**
	 * @param scrollResult the scrollResult to set
	 */
	public void setScrollResult(JScrollPane scrollResult) {
		this.scrollResult = scrollResult;
	}

	/**
	 * @return the btnCancel
	 */
	public JButton getBtnCancel() {
		return btnCancel;
	}

	/**
	 * @param btnCancel the btnCancel to set
	 */
	public void setBtnCancel(JButton btnCancel) {
		this.btnCancel = btnCancel;
	}

	/**
	 * @return the cbStatus
	 */
	@SuppressWarnings("rawtypes")
	public JComboBox getCbStatus() {
		return cbStatus;
	}

	/**
	 * @param cbStatus the cbStatus to set
	 */
	@SuppressWarnings("rawtypes")
	public void setCbStatus(JComboBox cbStatus) {
		this.cbStatus = cbStatus;
	}

	/**
	 * @return the rdbDateChooser
	 */
	public ButtonGroup getRdbDateChooser() {
		return rdbDateChooser;
	}

	/**
	 * @param rdbDateChooser the rdbDateChooser to set
	 */
	public void setRdbDateChooser(ButtonGroup rdbDateChooser) {
		this.rdbDateChooser = rdbDateChooser;
	}

	/**
	 * @return the rdbtnDateRequirement
	 */
	public JRadioButton getRdbtnDateRequirement() {
		return rdbtnDateRequirement;
	}

	/**
	 * @param rdbtnDateRequirement the rdbtnDateRequirement to set
	 */
	public void setRdbtnDateRequirement(JRadioButton rdbtnDateRequirement) {
		this.rdbtnDateRequirement = rdbtnDateRequirement;
	}

	/**
	 * @return the rdbtnDeadline
	 */
	public JRadioButton getRdbtnDeadline() {
		return rdbtnDeadline;
	}

	/**
	 * @param rdbtnDeadline the rdbtnDeadline to set
	 */
	public void setRdbtnDeadline(JRadioButton rdbtnDeadline) {
		this.rdbtnDeadline = rdbtnDeadline;
	}

	/**
	 * @return the rdbtnDateRelease
	 */
	public JRadioButton getRdbtnDateRelease() {
		return rdbtnDateRelease;
	}

	/**
	 * @param rdbtnDateRelease the rdbtnDateRelease to set
	 */
	public void setRdbtnDateRelease(JRadioButton rdbtnDateRelease) {
		this.rdbtnDateRelease = rdbtnDateRelease;
	}

	/**
	 * @return the lblDateFrom
	 */
	public JLabel getLblDateFrom() {
		return lblDateFrom;
	}

	/**
	 * @param lblDateFrom the lblDateFrom to set
	 */
	public void setLblDateFrom(JLabel lblDateFrom) {
		this.lblDateFrom = lblDateFrom;
	}

	/**
	 * @return the txtDateTo
	 */
	public JDateChooser getTxtDateTo() {
		return txtDateTo;
	}

	/**
	 * @param txtDateTo the txtDateTo to set
	 */
	public void setTxtDateTo(JDateChooser txtDateTo) {
		this.txtDateTo = txtDateTo;
	}

	/**
	 * @return the lblDateTo
	 */
	public JLabel getLblDateTo() {
		return lblDateTo;
	}

	/**
	 * @param lblDateTo the lblDateTo to set
	 */
	public void setLblDateTo(JLabel lblDateTo) {
		this.lblDateTo = lblDateTo;
	}

	/**
	 * @return the txtDateFrom
	 */
	public JDateChooser getTxtDateFrom() {
		return txtDateFrom;
	}

	/**
	 * @param txtDateFrom the txtDateFrom to set
	 */
	public void setTxtDateFrom(JDateChooser txtDateFrom) {
		this.txtDateFrom = txtDateFrom;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		RequirementHandler requirementHandler = new RequirementHandler();
		if(e.getSource() == btnSearch){
			requirementHandler.searchRequirements(this);
		} else if(e.getSource() == btnAdd){
			RequirementAddNewForm requirementAddNewForm = new RequirementAddNewForm();
			requirementHandler.loadServiceNameToServiceComboBox(requirementAddNewForm);
			requirementAddNewForm.setVisible(true);
		} else if (e.getSource() == btnRefresh){
			requirementHandler.loadTable(this);
		} else if (e.getSource() == btnEdit){
			requirementHandler.editRequirementForm(this);
		} else if(e.getSource() == btnDelete){
			requirementHandler.deleteRequirementFromTable(this);
		} else if (e.getSource() == btnClose){
			System.exit(0);
		}
	}

}
